Python Control Flow and Range Function
Python's control flow structures and the range() function are essential for managing how code executes based on conditions, loops, and sequences. This document provides a comprehensive overview of these concepts, complete with examples and outputs.
1. if, elif, and else Statements​
These conditional statements execute different blocks of code depending on whether specific conditions evaluate to True.
x = 10
if x > 0:
print("Positive") # Output: Positive
elif x == 0:
print("Zero")
else:
print("Negative")
- Explanation:
- If
x > 0, the program prints "Positive". - If
x == 0, it prints "Zero". - Otherwise, it defaults to printing "Negative".
- If
2. Nested Conditions​
You can nest one condition inside another for more complex decision-making logic.
x = 5
y = 10
if x < 10:
if y > 5:
print("x is less than 10 and y is greater than 5")
# Output: x is less than 10 and y is greater than 5
- Explanation:
- The outer
ifchecks ifx < 10. - Inside that block, the inner
ifchecks ify > 5. - Both conditions must be satisfied for the message to print.
- The outer
3. for Loops​
A for loop iterates over a sequence (like lists, tuples, strings) or a range of numbers.
# Iterating through a range
for i in range(5):
print(i)
# Output:
# 0
# 1
# 2
# 3
# 4
# Iterating through a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# Output:
# apple
# banana
# cherry
- Explanation:
- In the first example,
range(5)generates numbers from0to4. - In the second example, the loop iterates over each element in the
fruitslist and prints them.
- In the first example,
4. while Loops​
The while loop repeatedly executes as long as the given condition remains True.
count = 0
while count < 3:
print("Count:", count)
# Output:
# Count: 0
# Count: 1
# Count: 2
count += 1
- Explanation:
- The loop starts with
count = 0. - It continues executing until
countreaches3. - After each iteration,
countis incremented by1.
- The loop starts with
5. break and continue​
break: Exits the loop prematurely when a specific condition is met.continue: Skips the rest of the current iteration and moves to the next one.
# Using break
for i in range(5):
if i == 3:
break # Exit the loop when i equals 3
print(i)
# Output:
# 0
# 1
# 2
# Using continue
for i in range(5):
if i == 3:
continue # Skip iteration when i equals 3
print(i)
# Output:
# 0
# 1
# 2
# 4
- Explanation:
- In the
breakexample, the loop stops wheni == 3, so only values0,1, and2are printed. - In the
continueexample, the loop skips printing3but continues with subsequent iterations.
- In the
6. else with Loops​
The else block executes after the loop completes normally (i.e., without hitting a break).
for i in range(3):
print(i)
else:
print("Loop finished")
# Output:
# 0
# 1
# 2
# Loop finished
- Explanation:
- The
elseblock runs after theforloop finishes all its iterations. - If a
breakstatement were used inside the loop, theelseblock would not execute.
- The
7. pass Statement​
The pass statement is a no-operation placeholder. It does nothing but allows you to define empty blocks where Python expects some code.
for i in range(5):
if i == 2:
pass # Placeholder; does nothing
print(i)
# Output:
# 0
# 1
# 2
# 3
# 4
- Explanation:
- When
i == 2, thepassstatement ensures that the block doesn't cause an error due to being empty. - All numbers from
0to4are printed regardless of thepass.
- When
8. The range() Function​
The range() function generates a sequence of numbers for use in loops. It can take up to three arguments: start, stop, and step.
Basic Usage (range(stop))​
for i in range(5):
print(i)
# Output:
# 0
# 1
# 2
# 3
# 4
- Explanation:
- The
range(5)generates numbers from0to4(the upper limit5is exclusive).
- The
Specifying Start and Stop (range(start, stop))​
for i in range(1, 6):
print(i)
# Output:
# 1
# 2
# 3
# 4
# 5
- Explanation:
- The
range(1, 6)generates numbers starting from1and stops before6.
- The
Adding a Step Value (range(start, stop, step))​
# Incrementing with a step
for i in range(1, 6, 2):
print(i)
# Output:
# 1
# 3
# 5
# Decrementing with a negative step
for i in range(5, 0, -1):
print(i)
# Output:
# 5
# 4
# 3
# 2
# 1
- Explanation:
- The first example increments by
2, resulting in1, 3, 5. - The second example decrements by
1, resulting in5, 4, 3, 2, 1.
- The first example increments by
Practical Use Cases of range()​
Iterating Over Indices of a List​
fruits = ["apple", "banana", "cherry"]
for i in range(len(fruits)):
print(f"Index {i}: {fruits[i]}")
# Output:
# Index 0: apple
# Index 1: banana
# Index 2: cherry
- Explanation:
range(len(fruits))generates indices0, 1, 2, which are used to access each element of the list.
Skipping Elements in a Sequence​
numbers = [10, 20, 30, 40, 50, 60]
for i in range(0, len(numbers), 2):
print(numbers[i])
# Output:
# 10
# 30
# 50
- Explanation:
- The
range(0, len(numbers), 2)generates indices0, 2, 4, corresponding to10, 30, 50.
- The
Common Pitfalls with range()​
Issue 1: Forgetting That stop Is Exclusive​
for i in range(1, 5):
print(i)
# Output:
# 1
# 2
# 3
# 4
- Explanation:
- The
range(1, 5)generates numbers1, 2, 3, 4but does not include5.
- The
Issue 2: Incorrect Step Values​
for i in range(1, 5, -1):
print(i)
# Output: (No output)
- Explanation:
- The
range(1, 5, -1)tries to count backward from1to5, which is impossible.
- The
Conclusion​
This document covers Python's control flow structures (if, elif, else, loops, break, continue, else, and pass) and the versatile range() function. Understanding these concepts is crucial for writing efficient, readable, and dynamic Python programs.